﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages
{
    public partial class ErrorPage : UserControl
    {
        public String ErrorTitle
        {
            get { return (String)GetValue(ErrorTitleProperty); }
            set { SetValue(ErrorTitleProperty, value); }
        }
        public static readonly DependencyProperty ErrorTitleProperty = 
            DependencyProperty.Register("ErrorTitle", typeof(String), typeof(ErrorPage), new PropertyMetadata(String.Empty));

        public String FriendlyMessage
        {
            get { return (String)GetValue(FriendlyMessageProperty); }
            set { SetValue(FriendlyMessageProperty, value); }
        }
        public static readonly DependencyProperty FriendlyMessageProperty = 
            DependencyProperty.Register("FriendlyMessage", typeof(String), typeof(ErrorPage), new PropertyMetadata(String.Empty));

        public int ErrorCode
        {
            get { return (int)GetValue(ErrorCodeProperty); }
            set { SetValue(ErrorCodeProperty, value); }
        }
        public static readonly DependencyProperty ErrorCodeProperty = 
            DependencyProperty.Register("ErrorCode", typeof(int), typeof(ErrorPage), new PropertyMetadata(0));

        public String ErrorMessageDetail
        {
            get { return (String)GetValue(ErrorMessageDetailProperty); }
            set { SetValue(ErrorMessageDetailProperty, value); }
        }
        public static readonly DependencyProperty ErrorMessageDetailProperty = 
            DependencyProperty.Register("ErrorMessageDetail", typeof(String), typeof(ErrorPage), new PropertyMetadata(String.Empty));

        public String StackTrace
        {
            get { return (String)GetValue(StackTraceProperty); }
            set { SetValue(StackTraceProperty, value); }
        }
        public static readonly DependencyProperty StackTraceProperty = 
            DependencyProperty.Register("StackTrace", typeof(String), typeof(ErrorPage), new PropertyMetadata(String.Empty));

        public String AdministratorEmail { get; set; }

        private Popup m_oPopup;

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorPage" /> class.
        /// </summary>
        public ErrorPage()
        {
            // Required to initialize variables
            InitializeComponent();

            m_oPopup = new Popup()
            {
                Child = this,
            };

            FriendlyMessage = Resx.GetLabel(ResxCommon.FRIENDLY_MESSAGE);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorPage" /> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="detail">The detail.</param>
        public ErrorPage(string message, string stack, string caller)
            :this()
        {
            this.ErrorMessageDetail = string.Format("{0} - {1}", caller, message);
            this.StackTrace = stack;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorPage" /> class.
        /// </summary>
        /// <param name="ex">The ex.</param>
        public ErrorPage(Exception ex, string caller)
            : this()
        {
            this.ErrorMessageDetail = string.Format("{0} - {1}", caller,  ex.Message);
            this.StackTrace = ex.StackTrace;
        }


        /// <summary>
        /// Shows this instance.
        /// </summary>
        public void Show()
        {
            Height = Application.Current.Host.Content.ActualHeight;
            Width = Application.Current.Host.Content.ActualWidth;

            m_oPopup.Height = Application.Current.Host.Content.ActualHeight;
            m_oPopup.Width = Application.Current.Host.Content.ActualWidth;

            m_oPopup.IsOpen = true;
        }

        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close()
        {
            m_oPopup.IsOpen = false;
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}